﻿using System;
using System.Text;
using Core.Library;
using DataObject;
using DataObject.Category;
using DataObject.Image;
using DataObject.Product;

namespace KMT.Controls.Product
{
    public partial class ListPaginationFilter : System.Web.UI.UserControl
    {        
        protected int PageIndex;
        protected int PageSize;        
        protected int TotalRecords;

        protected string CategoryId;
        protected string PropertyValueId;        
        protected string OrderBy;        

        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryId      = Utils.GetIdFromRequestString(Utils.CategoryId, string.Empty);
            PropertyValueId = Utils.GetIdFromRequestString(Utils.PropertyValueId, string.Empty);
            OrderBy         = Utils.GetIdFromRequestString(Utils.OrderBy, string.Empty);

            PageIndex  = Utils.GetIdFromRequestString(Utils.PageIndex, SettingValues.PageIndex);
            PageSize   = Utils.GetIdFromRequestString(Utils.PageSize, ConvertUtility.ToInt32(ddlPageSize.SelectedValue));
            
            if (!IsPostBack)
            {
                ListFilterProduct();            
            }
        }        

        private void ListFilterProduct()
        {
            var sb = new StringBuilder();

            var lstProduct = ProductController.Product_ListPagination_Filter(CategoryId, PropertyValueId, OrderBy, PageIndex, PageSize, ref TotalRecords);
            if (lstProduct != null && lstProduct.Rows.Count > 0)
            {
                ltPager.Text = PagerUtility.PageLinks(PageIndex, PageSize, TotalRecords, SettingValues.PageStep, "{0}");

                //UrlHelpers.ListProductPaginationByCategory(CategoryId, CategoryName, "{0}")
                var isNext = -1;
                var isCount = 1;

                for (int i = 0; i < lstProduct.Rows.Count; i++)
                {
                    var productId     = ConvertUtility.ToInt32(lstProduct.Rows[i]["ProductId"]);
                    var status        = ConvertUtility.ToInt32(lstProduct.Rows[i]["Status"]);
                    var specialPrice  = ConvertUtility.ToDecimal(lstProduct.Rows[i]["SpecialPrice"]);
                    var unitPrice     = ConvertUtility.ToDecimal(lstProduct.Rows[i]["UnitPrice"]);
                    var productName   = lstProduct.Rows[i]["Name"].ToString();
                    var unsignProduct = lstProduct.Rows[i]["UnsignProduct"].ToString();

                    var unsignCategory = CategoryController.GetUnsignCategoryName(ConvertUtility.ToInt32(lstProduct.Rows[i]["CategoryId"]));

                    var linkHref = UrlHelpers.ProductDetail(unsignCategory, productId, unsignProduct);
                        
                    if (i % 3 == 0)
                    {
                        sb.Append("<ul class=\"products-grid columns3\">");
                        isNext = isNext + 3;
                    }

                    sb.Append((isCount > 0 && isCount % 3 == 0) ? "<li class=\"item last\">" : "<li class=\"item\">");

                    if (status == (int) ProductStatus.Highlight)
                        sb.Append("<div class=\"newproduct_grid\">New</div>");

                    var lstImage = ImageController.Image_GetByProductId(productId);
                    if (lstImage.Count > 2)
                    {                            
                        sb.AppendFormat("<a href=\"{0}\" title=\"{1}\" class=\"product-image\">", linkHref, productName);                                                        
                        var imgHref1 = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size={3}",
                                                    Constants.ThumbImage, lstImage[0].ImageId, (int)CategoryType.Image, Constants.ThumbImageSizeListFilter);
                        var imgHref2 = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size={3}",
                                                    Constants.ThumbImage, lstImage[1].ImageId, (int)CategoryType.Image, Constants.ThumbImageSizeListFilter);
                        sb.AppendFormat("<img src=\"{0}\" alt=\"{1}\" />", imgHref1, productName);
                        sb.AppendFormat("<img src=\"{0}\" onmouseover=\"hideImage(this)\" onmouseout=\"showImage(this)\" alt=\"{1}\" />", imgHref2, productName);                            
                        sb.Append("</a>");
                    }                   
                    else
                    {
                        sb.AppendFormat("<a href=\"{0}\" title=\"{1}\" class=\"product-image\">", linkHref,
                                            productName);
                        var imgHref = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size={3}",
                                                    Constants.ThumbImage, productId, (int)CategoryType.Product, Constants.ThumbImageSizeListFilter);                            
                        sb.AppendFormat("<img src=\"{0}\" alt=\"{1}\" />", imgHref, productName);                            
                        sb.Append("</a>");
                    }

                    sb.Append("<div class=\"moreinfo\">");
                    sb.AppendFormat("<h2 class=\"product-name\"><a href=\"{0}\" title=\"{1}\">{1}</a></h2>", linkHref, productName);
                    sb.Append("<div class=\"price-box\">");

                    if (specialPrice > 0)
                    {
                        sb.Append("<p class=\"old-price\">");
                        sb.AppendFormat("<span class=\"price-label\">Giá cũ:</span> <span class=\"price\">{0}</span>", Utils.FormatPrice(unitPrice));
                        sb.Append("</p>");

                        sb.Append("<p class=\"special-price\">");
                        sb.AppendFormat(
                            "<span class=\"price-label\">Giá đặc biệt:</span> <span class=\"price\">{0}</span>", Utils.FormatPrice(specialPrice));
                        sb.Append("</p>");
                    }
                    else
                    {
                        sb.Append("<p class=\"special-price\">");
                        sb.AppendFormat("<span class=\"price-label\">Giá:</span> <span class=\"price\">{0}</span>", Utils.FormatPrice(unitPrice));
                        sb.Append("</p>");
                    }

                    sb.Append("</div>");
                    sb.Append("<button type=\"button\" title=\"Đặt hàng\" class=\"button btn-cart\"><span><span>Đặt hàng</span></span></button>");
                    sb.Append("<div class=\"clear\"></div>");
                    sb.Append("</div>");
                    sb.Append("</li>");

                    if (i == isNext)
                        sb.Append("</ul>");

                    isCount++;
                }

                ltProduct.Text = sb.ToString();
            }            
        }
    }
}